<?php
class UserModel extends BaseModel
{

    //自动验证
    protected $_validate = array(
        array('num', 'require', '工号必须'),
        array('email', 'require', '邮箱必须'),
        array('num', '', '工号已经存在', 0, 'unique', 3),
        array('email', '', '邮箱已经存在', 0, 'unique', 3),
        array('email', 'email', '邮箱格式不符合要求'), //邮箱不符合@格式
    );

    /**
     * 获取用户基本信息
     * @param int $uid
     * @author ice <iceinto@mallog.com.cn>
     */
    public function getOne($uid)
    {
        return $this->getById($uid, 'id,num,is_auto_pull');
    }

    /**
     * 获取用户列表
     * @param array $map 查询条件
     * @param string $limit 分页条件
     * @param string $order 排序
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getList($map = null, $limit = null, $order = null)
    {
        $fields = 'id,num,email';
        return $this->getListByWhere($map, $fields, $limit, $order);
    }

    /**
     * Access to current user id number
     * @param int $uid user id
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getNum($uid)
    {
        $num = false;
        if(empty($uid)){
            return false;
        }
        $user = $this->getById($uid, 'num');
        if ($user) {
            $num = $user['num'];
        }
        return $num;
    }

    public function getUserList($map = null, $fields = 'id,num', $order = 'id DESC')
    {
        $allCount = $this->where($map)->count();
        $Page = $this->getPage($allCount);
        $data = $this->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->field($fields)
            ->order($order)
            ->select();
        return array('data' => $data, 'page' => $Page->show());
    }

    /**
     * Get email count
     * @param string $email
     * @param int $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getEmailCount($email, $uid = 0)
    {
        $map = array();
        $map['email'] = $email;
        if ($uid) {
            $map['id'] = array('neq', $uid);
        }
        return $this->getCount($map);
    }

    /**
     * Get NUM count
     * @param string $num
     * @param int $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getNumCount($num, $uid = 0)
    {
        $map = array();
        $map['num'] = $num;
        if ($uid) {
            $map['id'] = array('neq', $uid);
        }
        return $this->getCount($map);
    }

    /**
     * Get users of you created
     * @param int $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getChilds($uid)
    {
        $map = array();
        $map['fid'] = $uid;
        return $this->getUserList($map);
    }

    public function getChild($fid, $uid)
    {
        $map = array();
        $map['fid'] = $fid;
        $map['id'] = $uid;
        return $this->getByWhere($map, 'num');
    }

    /**
     * 获取美工等级
     * @param int $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getMLevel($uid)
    {
        $map = array();
        $map['m_role_user.user_id'] = $uid;
        $map['m_role_user.role_id'] = 3;
        return $this->field('m_user.level')
            ->join('INNER JOIN m_role_user ON m_user.id = m_role_user.user_id')
            ->where($map)
            ->find();
    }
}